﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Project.MVC.Model;
namespace Project.MVC.MsSQLDAL
{
    public class ContactRepository : IContactRepository
    {
        LinqProjectDataContext dataContext;
        public ContactRepository()
        {
            dataContext = new LinqProjectDataContext();
        }

        public IQueryable<ContactInfo> GetList()
        {
            return dataContext.Contacts.Select(record => new ContactInfo
                {
                    Id = record.Id,
                    Company = record.Company,
                    Address = record.Address,
                    Email = record.Email,
                    Phone = record.Phone,
                    Fax = record.Fax,
                    AboutUs = record.AboutUs,
                    Yahoo = record.Yahoo,
                    Sky = record.Sky,
                    Sologan = record.Sologan
                });
        }

        public bool Insert(ContactInfo value)
        {
            if (value == null)
                return false;
            try
            {
                var info = General.GetInfo(value);
                dataContext.Contacts.InsertOnSubmit(info);
                dataContext.SubmitChanges();
                value.Id = info.Id;
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool Update(ContactInfo value)
        {
            if (value == null)
                return false;
            try
            {
                //var info = GetById(value.Id);
                var info = dataContext.Contacts.Where(c => c.Id == value.Id).SingleOrDefault();
                if (info != null)
                {
                    info.Company = value.Company;
                    info.Address = value.Address;
                    info.Email = value.Email;
                    info.Phone = value.Phone;
                    info.Fax = value.Fax;
                    info.AboutUs = value.AboutUs;
                    info.Yahoo = value.Yahoo;
                    info.Sky = value.Sky;
                    info.Sologan = value.Sologan;
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool Delete(ContactInfo value)
        {
            if (value == null)
                return false;
            try
            {
                //var info = GetById(value.Id);
                var info = dataContext.Contacts.Where(c => c.Id == value.Id).SingleOrDefault();
                if (info != null)
                {
                    dataContext.Contacts.DeleteOnSubmit(info);
                    return true;
                }

                return false;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Count()
        {
            return dataContext.Contacts.Count();
        }

        public void Commit()
        {
            dataContext.SubmitChanges();
        }
    }
}
